package com.example.administrator.shzs.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.administrator.shzs.R;
import com.example.administrator.shzs.activity.BaseActivity;
import com.example.administrator.shzs.adapter.ShopDatrilsAdapter;
import com.example.administrator.shzs.app.MyApplication;
import com.example.administrator.shzs.entity.CollectShopData;
import com.example.administrator.shzs.entity.MerchantData.ListBean;
import com.example.administrator.shzs.fragment.shopdetails.DishesFragment;
import com.example.administrator.shzs.fragment.shopdetails.EvaluateFragment;
import com.example.administrator.shzs.fragment.shopdetails.MerchantFragment;
import com.example.administrator.shzs.url.Contents;
import com.example.administrator.shzs.utils.ToastUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;


/**
 * 商家详情页面
 */
public class ShopActivity extends BaseActivity implements View.OnClickListener {

    /**
     * 返回，搜索，分享，到店支付，收藏
     */
    private ImageView mIvBack, mIvSearch, mIvShare, mIvPay, mIvCollect;

    private TextView tvShopName;//商家名
    private CircleImageView ivShopImage;//商家图片

    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private List<Fragment> mFragments;

    //商家界面标题名
    private List<String> mTitls;

    private ShopDatrilsAdapter mAdapter;

    /**
     * 商家ID
     */
    public String shopId;

    /**
     * 商家名
     */
    private String shopName;

    /**
     * 商家图片
     */
    private String shopImage;

    private String sendfree;//配送费

    /**
     * 是否是从订单页面过来的
     */
    private boolean isAllOrderFragment=false;

    /**
     * 是否是从收藏的点击界面跳转过来的
     */
    private boolean isAttentionShopActivity=false;

    /**
     * 默认没有收藏
     */
    private boolean isCollect=false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop);
        MyApplication.app.activityList.add(this);

        //初始化
        setViews();

        setData();

        //监听器
        setListeners();

        //适配器
        setAdapter();

    }



    /**
     * 获取数据
     */
    private void setData() {
        //获取数据
        Intent intent = getIntent();
        isAllOrderFragment=intent.getBooleanExtra("isAllOrderFragment",false);
        isAttentionShopActivity=intent.getBooleanExtra("AttentionShopActivity",false);

        if(isAllOrderFragment){//是从订单界面过来的
            shopId=intent.getStringExtra("shopId");
            MyApplication.app.setShopId(shopId);
            shopImage=intent.getStringExtra("shopImage");
            shopName=intent.getStringExtra("shopName");
            sendfree=intent.getStringExtra("sendfree");

        }else if(isAttentionShopActivity){//收藏的店家界面

            CollectShopData.ListBean data= (CollectShopData.ListBean) intent.getSerializableExtra("ListBean");
            // 商家信息
            shopId = data.getDataID();//商家id
            MyApplication.app.setShopId(shopId);
            shopImage = data.getIcon();//商家图片
            shopName = data.getTogoName();//商家店名
            sendfree = data.getSendmoney();//配送费

        } else {
            //首页跳转过来的
            ListBean data = (ListBean) intent.getSerializableExtra("MerchantData");
            // 商家信息
            shopId = data.getDataID();//商家id
            MyApplication.app.setShopId(shopId);
            shopImage = data.getIcon();//商家图片
            shopName = data.getTogoName();//商家店名
            sendfree = data.getSendmoney();//配送费
        }

        //设置商家名和图片
        tvShopName.setText(shopName == null ? "商家" : shopName);
        if (shopImage != null) {
            DisplayImageOptions options = new DisplayImageOptions.Builder()
                    .showImageOnLoading(R.mipmap.home_icon1_03)
                    .showImageForEmptyUri(R.mipmap.home_icon1_03)
                    .showImageOnFail(R.mipmap.home_icon1_03)
                    .cacheInMemory(true)
                    .cacheOnDisk(true)
                    .build();
            ImageLoader.getInstance().displayImage(shopImage, ivShopImage, options);

        }
    }
//配送费
    public String getsendfree() {
        return sendfree;
    }

    public String getShopName() {
        return shopName;
    }

    /**
     * 适配器
     */
    private void setAdapter() {

        mFragments = new ArrayList<>();
        mFragments.add(new DishesFragment());
        mFragments.add(new EvaluateFragment());
        mFragments.add(new MerchantFragment());

        //标题
        mTitls = new ArrayList<>();
        mTitls.add("点菜");
        mTitls.add("评价");
        mTitls.add("商家");

        mAdapter = new ShopDatrilsAdapter(getSupportFragmentManager(), mFragments, this, mTitls);
        mViewPager.setAdapter(mAdapter);

        mTabLayout.setupWithViewPager(mViewPager);

    }

    /**
     * 获取商家ID
     *
     * @return
     */
    public String getShopId() {
        return shopId;
    }


    public String getShopImage() {
        return shopImage;
    }

    /**
     * 监听器
     */
    private void setListeners() {

        mIvBack.setOnClickListener(this);
        mIvSearch.setOnClickListener(this);
        mIvShare.setOnClickListener(this);
        mIvPay.setOnClickListener(this);
        mIvCollect.setOnClickListener(this);

    }


    /**
     * 初始化
     */
    private void setViews() {

        mIvBack = (ImageView) findViewById(R.id.iv_back_shopactivity);
        mIvSearch = (ImageView) findViewById(R.id.iv_search_shopactivity);
        mIvShare = (ImageView) findViewById(R.id.iv_share_shopactivity);
        mIvPay = (ImageView) findViewById(R.id.iv_pay_shopactivity);
        mIvCollect = (ImageView) findViewById(R.id.iv_collect_shopactivity);

        ivShopImage = (CircleImageView) findViewById(R.id.iv_shopImage_shopactivity);

        tvShopName = (TextView) findViewById(R.id.tv_shop_name_shoppactivity);

        mViewPager = (ViewPager) findViewById(R.id.vp_details_shopactivity);
        mTabLayout = (TabLayout) findViewById(R.id.tab_layout_shopactivity);
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.iv_back_shopactivity://返回

                finish();

                break;

            case R.id.iv_search_shopactivity://搜索


                Intent intents = new Intent(ShopActivity.this, SearchActivity.class);
                startActivity(intents);


                break;


            case R.id.iv_share_shopactivity://分享


                break;


            case R.id.iv_collect_shopactivity://收藏


                if(!isCollect){
                    collectShop(1);
                    isCollect=true;
                }else {
                    collectShop(-1);
                    isCollect=false;
                }

                break;


            case R.id.iv_pay_shopactivity://线下支付

                Intent intent = new Intent(this, OfflinePayActivity.class);
                intent.putExtra("ShopName", shopName);
                intent.putExtra("ShopImage", shopImage);
                intent.putExtra("ShopId", shopId);
                startActivity(intent);


                break;

        }
    }


    /**
     * 收藏
     */
    private void collectShop(int op) {

        String url= Contents.COLLECT_SHOP_URL+"?userid="+MyApplication.app.getUserId()+"&op="+op+"&togoid="+shopId;

        RequestParams params=new RequestParams(url);
        Log.e("Life","收藏参数"+params.toString());
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                Log.e("Life","收藏"+result);
                try {

                    JSONObject obj=new JSONObject(result);

                    int state = obj.getInt("state");
                    String msg = obj.getString("msg");
                    if(state==1){
                        ToastUtils.showMessageShort(ShopActivity.this,msg);
                    }else {
                        ToastUtils.showMessageShort(ShopActivity.this,msg);
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ex.printStackTrace();
            }
            @Override
            public void onCancelled(CancelledException cex) {
            }
            @Override
            public void onFinished() {
            }
        });
    }

}
